﻿using System;
using System.Linq;
using DBAccessLibrary.Common;
using System.Collections.ObjectModel;
using System.Xml.Serialization;

namespace DBAccessLibrary.Model
{
    [Serializable]
    [XmlInclude(typeof(ServerEntity))]
    [XmlInclude(typeof(DBEntity))]
    [XmlInclude(typeof(TableEntity))]
    [XmlInclude(typeof(FieldEntity))]
    [XmlInclude(typeof(ViewEntity))]
    [XmlInclude(typeof(ProcEntity))]
    [XmlInclude(typeof(DescriptionEntity))]
    public class DataBaseEntity : BasePropertyChange
    {
        private string _name;
        private string _chName = string.Empty;
        private string _imageUrl;
        private bool _isExpanded;
        private bool _isSelected;
      
        private ObservableCollection<DataBaseEntity> _childNodes;
       
        public DataBaseEntity(string name)
        {
            _childNodes = new ObservableCollection<DataBaseEntity>();
            ImageUrl = "/Images/Tree/WordWrap.png";
            _name = name;
        }

        /// <summary>
        /// 名称
        /// </summary>
        public string Name
        {
            get { return _name; }
            set 
            {
                if (value != _name)
                {
                    _name = value;
                    this.NotifyPropertyChanged(o => o.Name);
                }
            }
        }

        public string ChName
        {
            get { return _chName; }
            set
            {
                if (value != _chName)
                {
                    _chName = value;
                    this.NotifyPropertyChanged(o => o.ChName);
                }
            }
        }

        /// <summary>
        /// 图标地址
        /// </summary>
        public string ImageUrl
        {
            get { return _imageUrl; }
            set
            {
                if (value != _imageUrl)
                {
                    _imageUrl = value;
                    this.NotifyPropertyChanged(o => o.ImageUrl);
                }
            }
        }

        /// <summary>
        /// 是否展开
        /// </summary>
        public bool IsExpanded 
        {
            get { return _isExpanded; }
            set 
            {
                if (value != _isExpanded)
                {
                    this._isExpanded = value;
                   
                    this.NotifyPropertyChanged(o => o.IsExpanded);
                }
            }
        }

        /// <summary>
        /// 是否选中
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    this._isSelected = value;
                    this.NotifyPropertyChanged(o => o.IsSelected);
                }
            }
        }
        /// <summary>
        /// 子节点
        /// </summary>
        public ObservableCollection<DataBaseEntity> ChildNodes
        {
            get { return _childNodes; }
            set
            {
                if (value != _childNodes)
                {
                    _childNodes = value;
                    this.NotifyPropertyChanged(o => o.ChildNodes);
                }
            }
        }

        /// <summary>
        /// 子节点个数
        /// </summary>
        public int Count
        {
            get { return ChildNodes.Count; }
        }
        /// <summary>
        /// 是否 转换名称和备注
        /// </summary>
        public bool IsTrans { get; set; }

        /// <summary>
        /// 中文转换
        /// </summary>
        public void TransForm()
        {
            foreach (DataBaseEntity entity in ChildNodes)
            {
                string text = entity.ChName;
                entity.ChName = entity.Name;
                entity.Name = text;
                entity.IsTrans = !entity.IsTrans;
            }
        }
        /// <summary>
        /// 获取中文名称
        /// </summary>
        public string TransChName
        {
            get
            {
                if (IsTrans)
                {
                    return Name;
                }
                return ChName;
            }
            set
            {
                if (IsTrans)
                {
                    Name = value;
                }
                else
                {
                    ChName = value;
                }
            }
        }
        /// <summary>
        /// 获取字段名称
        /// </summary>
        public string TransName
        {
            get
            {
                if (IsTrans)
                    return ChName;
                return Name;
            }
            set
            {
                if (IsTrans)
                {
                    ChName =value;
                }
                else
                {
                    Name =value;
                }
            }
        }

        /// <summary>
        /// 撤销状态
        /// </summary>
        public virtual void ClearState()
        {
            this.IsExpanded = false;
            this.IsSelected = false;
            foreach (DataBaseEntity d in ChildNodes.ToArray())
            {
                d.ClearState();
            }
        }

        public virtual DataBaseEntity Copy()
        { return null; }
        protected void Copy(DataBaseEntity entity)
        {
            entity.IsTrans = this.IsTrans;
            entity.ImageUrl = this.ImageUrl;
            entity.ChName = this.ChName;
        }
    }
}
